// component/spellBuy/spellBuy.js
// var util = require('../../utils/util')   //引入配置公共请求接口
var configUrl = require('../../../config/config')  //引入配置接口文件
const app=getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data:{
      type: Object,
      // 数据类型
      value:{}
      // 数据值
    }
  },
  options: {
    styleIsolation: 'apply-shared'
  },
  /**
   * 组件的初始数据
   */
  data: {
    imgUrl:getApp().globalData.imgUrl,
    seckillsubColor: '#00AE97',
    seckillBgimg: '' , // 秒杀模块背景图
    seckillData: [], // 秒杀
    countDown: { day : "00", hour: "00", min: "00", sec: "00" },     // 秒杀倒计时
  },
  attached: function(){
    let that = this
    if (that.properties.data.mmsSpellBuyDTO.status == 'ongoing') {
      that.setData({
        endTime: that.properties.data.mmsSpellBuyDTO.endTime
      })
      that.countDown()
    }
    if (that.properties.data.mmsSpellBuyDTO.status == 'unStart') {
      that.setData({
        endTime: that.properties.data.mmsSpellBuyDTO.startTime
      })
      that.countDown()
    }
    // that.setData({
    //   endTime: that.properties.data.mmsSpellBuyDTO.endTime
    // })
    // that.countDown()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 倒计时
     */
    countDown:function(){
      let that = this;
      that.setData({
        timer: setInterval(function(){
          var lefttime = parseInt((new Date(that.data.endTime.replace(/-/g,"/")).getTime() - new Date().getTime()));
          if(lefttime <= 0) {
            that.setData({
              countDown:{day:"00",hour:"00",min:"00",sec:"00"}
            })
            clearInterval(that.data.timer);
            return;
          }

          var d = parseInt(lefttime /1000 /3600 /24);  //天数
          var h = parseInt(lefttime / 1000 / 3600 % 24); //小时
          var m = parseInt(lefttime / 1000 / 60 % 60);    //分钟
          var s = parseInt(lefttime / 1000  % 60);        //当前的秒

          d < 10 ? d = "0" + d : d;
          h < 10 ? h = "0" + h : h;
          m < 10 ? m = "0" + m : m;
          s < 10 ? s = "0" + s : s;

          that.setData({
            countDown:{ day: d, hour: h, min: m, sec:s }
          })
        },1000)
      })
    },
    goGroupActivity(data) {
      let id = data.currentTarget.dataset.id
      wx.navigateTo({
        url: '/packageLive/pages/spellBuy/groupActivity/groupActivity?id='+id
      })
    },
  }
})
